package com.kitri.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.kitri.db.SetOfDataSource;
import com.kitri.domain.JuctionReMb;

public class JuctionReMbDaoImpl implements JuctionReMbDao {

	@Override
	public void add(JuctionReMb juctionReMb) {
		StringBuffer sql = new StringBuffer();

		sql.append("INSERT INTO Juction_Re_Mb ");
		sql.append("VALUES(?, ?)");

		JdbcTemplate jdbcTemplate = new JdbcTemplate(
				SetOfDataSource.getDataSource());
		if (checkIt(juctionReMb)) {
			jdbcTemplate.update(
					sql.toString(),
					new Object[] { juctionReMb.getBoardSeq(), juctionReMb.getSeqId() });
		}
	}

	@Override
	public void remove(JuctionReMb juctionReMb) {
		StringBuffer sql = new StringBuffer();

		sql.append("DELETE FROM Juction_Re_Mb ");
		sql.append("WHERE BOARD_SEQ = ? ");
		sql.append("AND SEQ_ID = ?");

		JdbcTemplate jdbcTemplate = new JdbcTemplate(
				SetOfDataSource.getDataSource());
		jdbcTemplate.update(
				sql.toString(),
				new Object[] { juctionReMb.getBoardSeq(), juctionReMb.getSeqId() });
	}

	@Override
	public List<JuctionReMb> list() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean checkIt(JuctionReMb juctionReMb) {
		String sql = "SELECT * FROM Juction_Re_Mb WHERE BOARD_SEQ = ? AND SEQ_ID = ?";

		final JuctionReMb juctionObject = new JuctionReMb();

		JdbcTemplate jdbcTemplate = new JdbcTemplate(
				SetOfDataSource.getDataSource());
		jdbcTemplate.query(sql, new Object[] { juctionReMb.getBoardSeq(),
				juctionReMb.getSeqId() }, new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				juctionObject.setBoardSeq(rs.getInt("BOARD_SEQ"));
				juctionObject.setSeqId(rs.getString("SEQ_ID"));
			}
		});

		if (juctionObject.getSeqId() == null)
			return true;
		return false;
	}

	@Override
	public void removeOfBoard(int boardSeq) {
		StringBuffer sql = new StringBuffer();

		sql.append("DELETE FROM Juction_Re_Mb ");
		sql.append("WHERE BOARD_SEQ = ?");


		JdbcTemplate jdbcTemplate = new JdbcTemplate(
				SetOfDataSource.getDataSource());
		jdbcTemplate.update(
				sql.toString(),
				new Object[] { boardSeq });
	}

}
